<template>
	<div>
		<div class="intro">
			<div class="container">
				<p>输入主题并选择人数&组数，分享页面给群好友</p>
				<p>系统随机分配给各个小伙伴组别</p>
			</div>
		</div>
		<div class="mainBox" v-if="!home.hidegg">
			<div class="container">
				<div class="inp">
					<u--input placeholder="请输入主题" border="surround" v-model="zhuti"></u--input>
				</div>
				<u-gap height="8" bgColor="#eee" marginTop="10" marginBottom="10"></u-gap>

				<u-cell-group :border="false">
					<u-cell icon="setting-fill" title="参与人数">
						<u-number-box slot="right-icon" integer v-model="n"></u-number-box>
					</u-cell>
					<u-cell icon="setting-fill" title="分组组数">
						<u-number-box slot="right-icon" integer v-model="z"></u-number-box>
					</u-cell>
				</u-cell-group>
				<div class="uploadImg">
					<h4>教室分布图</h4>
					<div class="inp">
						<u--input placeholder="请输入教室图片链接" border="surround" v-model="imglink"></u--input>
					</div>
					<div class="jsImgbox" v-if="imglink != ''">
						<image :src="imglink" mode="widthFix" @click="showImg(imglink)"></image>
					</div>
					<!-- <u-upload
						:fileList="imgList"
						@afterRead="afterRead"
						@delete="deletePic"
						name="4"
						multiple
						:maxCount="1"
					></u-upload> -->
				</div>
				<u-gap height="8" bgColor="transparent" marginTop="10" marginBottom="10"></u-gap>
				<u-button @click="make()" text="马上发布" color="linear-gradient(45deg, #feb692, #ea5455)"></u-button>
			</div>
			<u-toast ref="uToast"></u-toast>
		</div>
	</div>
</template>

<script>
	const db = wx.cloud.database()
	export default {
		name: 'headcomd',
		data() {
			return {
				wxCode: "",
				openid: "",
				zhuti: "",
				n: 2,
				z: 2,
				imgList: [
					
				],
				home:"",
				imglink: ""
			};
		},
		
		onLoad(options) {
			if (!options) {
				this.infoScene = 'default';
			} else {
				
			}
		},
		computed: {
			
		},
		mounted() {
			
		},
		watch: {
			
		},
		created() {
			let that = this;
			that.getHome()
			var openid = wx.getStorageSync("useropenid");
			console.log(openid)
			if(openid){
				that.openid = openid
			}else{
				that.getopenid()
			}
			
		},
		methods: {
			getopenid(){
				let that = this
				wx.cloud.callFunction({
					name: 'getopenid',
					data: {},
					complete: res => {
						console.log('callFunction result: ', res)
						that.saveOpenid(res.result.openid)
					}
				})
			},
			saveOpenid(openid){
				db.collection("user_info").where({
					openid: openid
				})
				.get({
					success: function(res){
						console.log('success: ', res)
						if(res.data.length == 0){
							db.collection("user_info").add({
							    data: {
									openid: openid,
									time: new Date()
							    },
								success: function(res) {
								    console.log("openid储存成功" + openid)
									wx.setStorageSync("useropenid",openid)
								}
							})
						}else{
							console.log("openid已储存" + openid)
							wx.setStorageSync("useropenid",openid)
						}
					}
				})
			},
			make(){
				let that = this
				if(!that.zhuti){
					that.showToast('error','请输入主题')
					return
				}
				if(that.n < 2){
					that.showToast('error','最少为2人')
					return
				}
				if(that.z < 1){
					that.showToast('error','最少为1组')
					return
				}
				
				
				wx.showToast({
					title: "生成中",
					icon: "loading",
					duration: 5000
				})
				let wyz = that.openid + "-" + new Date().getTime()
				let zwArr = []
				for(let i = 0; i < that.n; i++){
					zwArr.push({id: i, openid: null,change: false})
				}
				db.collection("paixu_fenzu").add({
				    data: {
						openid: that.openid,
						zhuti: that.zhuti,
						nums: that.n,
						zu: that.z,
						wyz: wyz,
						jsimg: that.imglink,
						zw: zwArr,
						time: new Date()
				    },
					success: function(res) {
					    console.log(res)
						uni.navigateTo({
							url: './fenzu_join?wyz=' + wyz
						})
					}
				})
				// wx.cloud.uploadFile({
				// 	cloudPath: new Date().getTime().toString(),
				// 	filePath: that.imgList[0].url,
				// 	success:function(res){
				// 		that.jsimg = res.fileID
						
				// 	},
				// 	fail: function(req){
				// 		console.log(req)
				// 	}
				// })
				
			},
			showToast(type, info) {
				this.$refs.uToast.show({
					type: type,
					icon: false,
					title: '失败主题',
					message: info,
				})
			},
			afterRead(e){
				let that = this
				wx.compressImage({
					src: e.file[0].url,
					quality: 20,
					success(res) {
						that.imgList = [
							{url: res.tempFilePath}
						]
						console.log(res)
					},
					fail(req) {
						console.log(req)
					}
				})
			},
			deletePic(){
				this.imgList = []
			},
			getHome(){
				let that = this
				db.collection('a_show_info')
					.get({
						success: function(res) {
							if (res.data.length >= 0) {
								that.home = res.data[0]
							}
						}
					});
			},
		}
	};
</script>

<style>
	@import url(./index.css);
</style>
